//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.utils
{
	import __AS3__.vec.Vector;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.events.ResizeEvent;
	import mx.managers.PopUpManager;
	
	import reader.core.ReaderModelLocator;
	import reader.events.FeedLoaderEvent;
	import reader.model.vo.BlogItemVO;
	import reader.model.vo.BlogSourceVO;
	import reader.view.components.FeedProgress;

	[Event(name="feedList", 
		 type="reader.events.FeedLoaderEvent")]
	[Event(name="feedFail", 
		 type="reader.events.FeedLoaderEvent")]
	[Event(name="listComplete", 
		 type="reader.events.FeedLoaderEvent")]	
		 
	public class FeedLoaderList extends EventDispatcher
	{
		//////////////////////////////////////////////////////////////////////////////
		//Public properties
		
		public var isPause:Boolean;
		public var isUpdating:Boolean;
		public var blogItems:Vector.<BlogItemVO> = new Vector.<BlogItemVO>();
		
		private var _blogSource:Vector.<BlogSourceVO>;
		public function get blogSource():Vector.<BlogSourceVO>
		{
			return _blogSource;
		}
		public function set blogSource(value:Vector.<BlogSourceVO>):void
		{
			_blogSource = value;
			isUpdating = false;
			pointer = 0;
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//Private properties
		
		private const LIST_PER:uint = 5;
		
		private var interval:uint;
		private var timer:Timer;
		private var feedLoader:FeedLoader;		
		private var pointer:int;
		
		private var statusPanel:FeedProgress;
		private var airReader:Reader = ReaderModelLocator.instance.airReader;
		
		//////////////////////////////////////////////////////////////////////////////
		//Constructor
		public function FeedLoaderList(blogSource:Vector.<BlogSourceVO> = null, interval:uint = 1800000)
		{
			this._blogSource = blogSource;
			this.interval = interval;
			
			this.timer = new Timer(interval);
			this.timer.addEventListener(TimerEvent.TIMER, onTimerTick_handler);
			
			this.feedLoader = new FeedLoader();
			this.feedLoader.addEventListener(FeedLoaderEvent.FEED_LOAD, onFeedLoadComplete_handler);
			this.feedLoader.addEventListener(FeedLoaderEvent.FEED_FAIL, onFeedLoadError_handler);
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		public function start():void
		{
			this.timer.stop();
			if(!_blogSource || _blogSource.length == 0)
			{
				isUpdating = false;
				return;
			}
			if(statusPanel)
			{
				airReader.removeEventListener(ResizeEvent.RESIZE, onChangePosition_handler);
				PopUpManager.removePopUp(statusPanel);
			}
			statusPanel = PopUpManager.createPopUp(airReader, FeedProgress) as FeedProgress;
			onChangePosition_handler();
			airReader.addEventListener(ResizeEvent.RESIZE, onChangePosition_handler, false, 0, true);
			
			this.pointer = 0;
			this.timer.start();
			this.load();
		}
		
		public function stop():void
		{
			if(isUpdating)
			{
				blogItems = new Vector.<BlogItemVO>();
				isUpdating = false;
				this.timer.stop();
				this.timer.removeEventListener(TimerEvent.TIMER, onTimerTick_handler);
				this.feedLoader.removeEventListener(FeedLoaderEvent.FEED_LOAD, onFeedLoadComplete_handler);
				this.feedLoader.removeEventListener(FeedLoaderEvent.FEED_FAIL, onFeedLoadError_handler);
				if(statusPanel)
				{
					airReader.removeEventListener(ResizeEvent.RESIZE, onChangePosition_handler);
					PopUpManager.removePopUp(statusPanel);
				}
			}
		}
		
		public function pause():void
		{
			this.isPause = true;		
			this.timer.stop();
		}
		
		public function play():void
		{
			this.isPause = false;
			this.timer.start();
		}
		
		private function load():void
		{
			isUpdating = true;
			this.feedLoader.blogSource = _blogSource[pointer];
			if(statusPanel)
			{
				statusPanel.progressBar.setProgress(pointer, ReaderModelLocator.instance.blogSource.length);
			}
			this.feedLoader.loadFeed();
		}
		
		private function next():void
		{
			if(pointer < _blogSource.length - 1)
			{
				if(pointer >= LIST_PER && (pointer % LIST_PER) == 0)
				{
					var event:FeedLoaderEvent = new FeedLoaderEvent(FeedLoaderEvent.FEED_LIST);
					event.blogItems = blogItems;
					this.dispatchEvent(event);
					blogItems = new Vector.<BlogItemVO>();
				}
				this.pointer++;
				this.load();				
			}
			else
			{
				if(statusPanel)
				{
					airReader.removeEventListener(ResizeEvent.RESIZE, onChangePosition_handler);
					PopUpManager.removePopUp(statusPanel);
				}
				var evt:FeedLoaderEvent = new FeedLoaderEvent(FeedLoaderEvent.LIST_COMPLETE);
				evt.blogItems = blogItems;
				this.dispatchEvent(evt);
				blogItems = new Vector.<BlogItemVO>();
				isUpdating = false;
				this.pointer = 0;	
			}
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//events
		
		private function onFeedLoadComplete_handler(e:FeedLoaderEvent):void
		{
			var newBlog:Vector.<BlogItemVO> = BlogItemVO.getLatestBlogItem(e.blogSource.lastPubDate.getTime(), e.blogItems);
			if(newBlog.length > 0)
			{
				blogItems = blogItems.concat(newBlog);
				e.blogSource.totalBlogs += newBlog.length;
				e.blogSource.newBlogs += newBlog.length;
				e.blogSource.lastPubDate = newBlog[0].pubDate;
			}
			
			if(!isPause) this.next();
		}
		
		private function onFeedLoadError_handler(e:FeedLoaderEvent):void
		{
			this.dispatchEvent(new FeedLoaderEvent(FeedLoaderEvent.FEED_FAIL));
			
			if(!isPause) this.next();
				
		}
		
		private function onTimerTick_handler(e:TimerEvent):void
		{
			this.start();
		}
		
		private function onChangePosition_handler(e:Event = null):void
		{
			if(statusPanel)
			{
				statusPanel.x = airReader.width - statusPanel.width - 18;
				statusPanel.y = airReader.height - statusPanel.height - 15;
			}
		}
		
	}
}